﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.CourseOfLecture.Entity;
using PowerCreator.LMS.Web.CourseOfLecture;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Mavin;
using PowerCreator.LMS.Mavin.Entity;
public partial class sysadmin_CourseOfLectureManager_CourseOfLectureManager : PowerCreator.LMS.Web.Base.BasePage
{
    CourseOfLecture objCOL = new CourseOfLecture();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, 1);
        }
    }

    protected override void InitPage()
    {
        lbl_InfoTitle_ManagerCOL.Text = GetLanageValueByKey("lab_ManagerCOL_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_ManagerNew.Text = "新闻列表";
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        chkAll.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxCancel", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_DelCheck.Text = GetLanageValueByKey("btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        base.InitPage();
    }
    private string GetSearch()
    {
        string search = "";
        if (!string.IsNullOrEmpty(txtCOLTitle.Text))
            search = string.Format("COLTitle like '%{0}%'", txtCOLTitle.Text.Trim());
        return search;
    }

    protected void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objCOL.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
    }
    private void BinderPG(string search,int pageIndex)
    {
        string orderby = " COLID desc";
        DataTable dt = objCOL.Query(search, orderby, LMSPageSize, pageIndex);
        DataTable dt1 = new DataTable();
        VOTMavin vo = new VOTMavin();
        Mavin objMavin = new Mavin();
        dt.Columns.Add("COLAuthor");
        dt.Columns.Add("COLTypeStr");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["COLType"].ToString() == "0")
                dt.Rows[i]["COLTypeStr"] = "直播";
            else if (dt.Rows[i]["COLType"].ToString() == "1")
                dt.Rows[i]["COLTypeStr"] = "点播";
            if (dt.Rows[i]["FKCOLAuthorID"] != null)
            {
                if (dt.Rows[i]["FKCOLAuthorID"].ToString().Length > 0)
                {
                    vo = objMavin.Load(int.Parse(dt.Rows[i]["FKCOLAuthorID"].ToString()));
                    dt.Rows[i]["COLAuthor"] = vo.MavinName;
                }
            }

        }
        rptCOL.DataSource = dt;
        rptCOL.DataBind();

        PreAspNetPager(AspNetPager1);
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderPG(search,1);
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string url = "addCourseOfLecture.aspx";
        Script.RedirectURL(url);
    }
  
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPG(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rptCOL_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");

        if (e.CommandName.ToLower() == "leditor")
        {
            string url = "addCourseOfLecture.aspx?COLID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);

        }
        else if (e.CommandName.ToLower() == "ldel")
        {
            if (objCOL.Del(int.Parse(e.CommandArgument.ToString())))
            {
                string search = GetSearch();
                BinderPager(search);
                BinderPG(search, AspNetPager1.CurrentPageIndex);
            }
            else
            {
                lbl_msg.Text = GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
        }
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < rptCOL.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptCOL.Items[i].FindControl("Chbox_Check");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rptCOL.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptCOL.Items[i].FindControl("Chbox_Check");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_DelCheck_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < rptCOL.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptCOL.Items[i].FindControl("Chbox_Check");
                Label lab_COLID = (Label)rptCOL.Items[i].FindControl("lab_COLID");
                if (cbox.Checked)
                {
                    if (!objCOL.Del(int.Parse(lab_COLID.Text)))
                    {
                        Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
                    }

                }
            }
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }
}

